package com.wjzjq.hiring.common.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wjzjq.hiring.company.entity.CompanyEntity;
import com.wjzjq.hiring.talent.entity.TalentEntity;

public class AppUtil {
	public static boolean isLogin(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("talent");
		return obj == null ? false : true;
	}

	public static int getTalentId(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("talent");
		if (obj != null) {
			TalentEntity talent = (TalentEntity) obj;
			return talent.getId();
		} else {
			try {
				request.getRequestDispatcher("view/talent/login.jsp?sessionInvalidate=true").forward(request, null);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return 0;

	}

	public static CompanyEntity getCompany(HttpServletRequest request, HttpServletResponse response) {
		if (request.getSession().getAttribute("company") == null) {
			request.setAttribute("sessionClosed", "会话已经关闭！");
			try {
				request.getRequestDispatcher("view/company/company-login.jsp").forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
			throw new RuntimeException("会话已经关闭！获取Session中的内容失败");
		} else {
			return (CompanyEntity) request.getSession().getAttribute("company");
		}
	}

	/**
	 * 获取放到Session里面的Company的id
	 * 
	 * @param request
	 * @return
	 */
	public static int getCompanyId(HttpServletRequest request, HttpServletResponse response) {
		return getCompany(request, response).getId();
	}

}
